* Aggregate analyses

***** Beliefs

cd "$pathdata/Beliefs/"

use Beliefs_cleaned, clear


*Keep Baseline Rounds
keep if treatment == 0



replace bayesian_posterior = bayesian_posterior/100
replace belief = belief/100

gen delta=.
gen gamma=.
gen beta=.
gen default=.
gen alpha=.


**** Manually generate AIC for belief ~ N(bayesian_posterior, sigma^2)

* Generate Residuals
gen res = belief - bayesian_posterior

* Generate residual error
gen sigma2 = res*res
egen sigma = mean(sigma2)
replace sigma = sqrt(sigma)

* Generate likelihoods
gen like = normalden(res, 0, sigma)

*Generate Log-Likelihood
gen llike = ln(like)

*Generate AIC
egen AIC = total(llike)

replace AIC = -2*AIC

tab AIC


// * With CU
nl (belief = (1-min(1,max(0,(1-{gamma=1}*cognitive_uncertainty))))* {default=1} + min(1,max(0,(1-{gamma=1}*cognitive_uncertainty))) * bayesian_posterior), cl(id)
estat ic



*Drop outliers
drop if belief/bayesian_posterior > 10 | belief/bayesian_posterior < 0.1

// *Just mean CU by p
bysort bayes_round: egen cu = mean(cognitive_uncertainty)
nl (belief = (1-min(1,max(0,(1-{gamma=1}*cu)))) * {default=1} + min(1,max(0,(1-{gamma=1}*cu))) * bayesian_posterior), cl(id)
predict pred_cu_s
estat ic

// *Just mean CU
egen cu1 = mean(cognitive_uncertainty)
nl (belief = (1-min(1,max(0,(1-{gamma=1}*cu1)))) * {default=1} + min(1,max(0,(1-{gamma=1}*cu1))) * bayesian_posterior), cl(id)
predict pred_cu
estat ic


preserve
foreach i in pred_cu bayesian_posterior pred_cu_s belief {
	replace `i'=`i'*100
}


*Following Ben's instruction to round to the nearest 5
collapse (mean) belief pred_cu_s cognitive_uncertainty pred_cu (semean) se_ce=belief se_cu=pred_cu  (count) no = belief, by(bayes_round)

gen upper=belief+se_ce
gen lower=belief-se_ce
gen upper_cu=pred_cu+se_cu
gen lower_cu=pred_cu-se_cu

drop if no<30

tw (scatter belief bayes_round, msize(large)  ms(+) mcolor(black)) ///
	(line pred_cu_s bayes_round, lwidth(medthick) lcolor(green%80))  ///
 	(function y=x , range(0 100) lpattern(dash) lcolor(black) lwidth(thin) ), ///
	ytitle("Subjective Posterior Belief") ///
    xtitle("Bayesian Posterior") xscale( lcolor(none)) ysc(lcolor(none)) ///
	yline(0, lcolor(gs10) lwidth(thin)) ///
	graphregion(color(white)) ///
	ylabel(, tlc(none) angle(0) glcolor(gs15) glwidth(thin)) ///
	legend(order(1 2) label(1 "Subject Data") label(2 "Model Fit") r(2))
  graph export "~/Dropbox/Complexity_prefs/Experiments/Replication/Prolific/out/Beliefs/Figs/beliefs_structural_fits.pdf", replace


